package com.csx.saas.platform.init.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.csx.basic.base.service.SuperCacheServiceImpl;
import com.csx.basic.cache.model.CacheKeyBuilder;
import com.csx.basic.database.mybatis.conditions.Wraps;
import com.csx.saas.common.cache.init.InitPackageVersionCacheKeyBuilder;
import com.csx.saas.platform.init.dao.InitPackageVersionMapper;
import com.csx.saas.platform.init.entity.InitPackageVersion;
import com.csx.saas.platform.init.entity.InitVersionService;
import com.csx.saas.platform.init.service.InitPackageVersionService;
import com.csx.saas.platform.init.service.InitVersionServiceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.aop.framework.AopContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 业务实现类
 * 套餐版本
 * </p>
 *
 * @author ChenSongXin
 * @date 2021-02-28
 */
@Slf4j
@Service
@DS("#thread.tenant")
public class InitPackageVersionServiceImpl extends SuperCacheServiceImpl<InitPackageVersionMapper, InitPackageVersion> implements InitPackageVersionService {

    @Autowired
    private InitVersionServiceService initVersionServiceService;

    @Override
    protected CacheKeyBuilder cacheKeyBuilder() {
        return new InitPackageVersionCacheKeyBuilder();
    }

    protected InitPackageVersionService currentProxy() {
        return ((InitPackageVersionService) AopContext.currentProxy());
    }

    @Override
    public boolean save(InitPackageVersion model) {
        saveBatchVersionService(model.getServiceNbrs(),model.getVersionNbr());
        return super.save(model);
    }

    @Override
    public boolean updateById(InitPackageVersion model) {
        //清除授权的服务
        initVersionServiceService.remove(Wraps.<InitVersionService>lbQ().eq(InitVersionService::getVersionNbr, model.getVersionNbr()));
        saveBatchVersionService(model.getServiceNbrs(),model.getVersionNbr());
        return super.updateById(model);
    }

    @Override
    public InitPackageVersion getByIdCache(Serializable id) {
        InitPackageVersion initPackageVersion = super.getByIdCache(id);
        List<InitVersionService> list = initVersionServiceService.list(Wraps.<InitVersionService>lbQ().eq(InitVersionService::getVersionNbr, initPackageVersion.getVersionNbr()));
        List<String> serviceNbrs = list.stream().map(InitVersionService::getServiceNbr).collect(Collectors.toList());
        initPackageVersion.setServiceNbrs(serviceNbrs);
        return initPackageVersion;
    }

    /**
     * 添加套餐版本 服务功能关系
     * @param serviceNbrs
     * @param versionNbr
     */
    public void saveBatchVersionService(List<String> serviceNbrs, String versionNbr){
        if(null!=serviceNbrs && !serviceNbrs.isEmpty() && serviceNbrs.size() > 0){
            List<InitVersionService> list = serviceNbrs.stream().map(serviceNbr -> {
                InitVersionService initVersionService = InitVersionService.builder().serviceNbr(serviceNbr).versionNbr(versionNbr).build();
                return initVersionService;
            }).collect(Collectors.toList());
            initVersionServiceService.saveBatch(list);
        }
    }
}
